package com.yenlien.traceyun.usercenter.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.usercenter.dao.CompanyCustomAppVersionDao;
import com.yenlien.traceyun.usercenter.dao.CompanyDao;
import com.yenlien.traceyun.usercenter.dao.UserDao;
import com.yenlien.traceyun.usercenter.entity.Company;
import com.yenlien.traceyun.usercenter.entity.Resource;
import com.yenlien.traceyun.usercenter.entity.User;
import com.yenlien.traceyun.usercenter.service.ResourceService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-06-08
 * @description:
 **/

@RestController
@RequestMapping(value = "/resource")
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private CompanyCustomAppVersionDao companyCustomAppVersionDao;


    /**
     * 加载app对应的资源
     *
     * @return
     */
    @GetMapping(value = "/findByAppId/{appId}")
    public Object findByAppId(@PathVariable("appId") String appId) {
        return success(resourceService.findByAppId(appId));
    }

    /**
     * 加载app对应的资源
     *
     * @return
     */
    @GetMapping(value = "/findCustomAppVersion/{customAppVersionId}")
    public Object findByCustomAppVersionId(@PathVariable("customAppVersionId") String customAppVersionId) {
        return success(resourceService.loadCustomAppVerson(customAppVersionId));
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/dele/{id}")
    public Object dele(@PathVariable("id") String id) {
        resourceService.dete(id);
        return success("ok");
    }

    /**
     * 加载用户所有app的资源
     *
     * @return
     */
    @GetMapping(value = "/load/user/appmenus/{userId}")
    public Object loadAllAppMenu(@PathVariable("userId") String userId) {
        User user = userDao.findById(userId);
        if (user == null) {
            throw new BusinessException("500", "该用户不存在");
        }
        Company company = companyDao.findById(user.getCompanyId());
        if (company == null) {
            throw new BusinessException("500", "companyId不存在");
        }
        if (StringUtils.isEmpty(company.getUserId())) {
            throw new BusinessException("500", "企业未分配账号");
        }

        return success(resourceService.findAllMenus(userId, company.getId(), company.getUserId()));
    }

    /**
     * 加载用户所有app的资源
     *
     * @return
     */
    @GetMapping(value = "/load/company/appmenus/{companyId}")
    public Object loadAllCompanyAppMenu(@PathVariable("companyId") String companyId) {
        Company company = companyDao.findById(companyId);
        if (company == null) {
            throw new BusinessException("500", "companyId不存在");
        }
        if (StringUtils.isEmpty(company.getUserId())) {
            throw new BusinessException("500", "企业未分配账号");
        }
        return success(resourceService.findAllMenus(company.getUserId()));
    }

    @PostMapping(value = "/set/company/appmenus")
    public Object setCompanyAppmenus(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"companyId"});
        Company company = companyDao.findById(params.getString("companyId"));
        if (company == null) {
            throw new BusinessException("500", "companyId不存在");
        }
        if (StringUtils.isEmpty(company.getUserId())) {
            throw new BusinessException("500", "企业未分配账号");
        }
        params.put("userId", company.getUserId());
        return success(resourceService.setCompanyAppMenu(params));
    }


    @PostMapping(value = "/set/user/appmenus")
    public Object setUserAppmenus(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"userId"});
        return success(resourceService.setCompanyAppMenu(params));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody Resource resource) throws Exception {
        resourceService.add(resource);
        return success("success");
    }
}
